<?php
/**
 *  算法推荐
 */

namespace Biz\service;

use Doraemon\pockets\config\IniConfig as pIniConfig;
use Doraemon\pockets\constInterface\ApiCode;
use Doraemon\tools\Curl as tCurl;
use Doraemon\tools\Log as tLog;
/**
 * @deprecated
 */
class Recommend implements ApiCode
{
    const _CONFIG_FILE_ = "recommendation_api";
    const _CONFIG_SELECT_ = "biz";

    /**
     * @desc 推荐算法域名
     * @param string $key
     * @return array
     * @throws \Exception
     */
    private static function getDomain($key = 'cart_recommend')
    {
        $domain = pIniConfig::getConfigSelect(self::_CONFIG_FILE_, self::_CONFIG_SELECT_);
        if (!isset($domain[$key])) {
            throw new \Exception('未配置推荐算法地址', ApiCode::API_CODE_EXCEPTION);
        }
        return $domain[$key];
    }


    /**
     * @desc 错误日志
     * @param string $title 日志名称
     * @param array $params 存储参数
     * @return mixed
     */
    private static function errorLog($title, array $params)
    {
        tLog::warning('推荐算法::' . $title, $params);
    }

    /**
     * @desc 购物车推荐算法
     * @param int $uid 用户id
     * @param int $length 推荐数量
     * @return array
     */
    public static function cartRecommendation($uid, $length)
    {
        $response = [];
        $rs = [];
        try {
            $domain = self::getDomain('cart_recommend');
            $url = $domain . '?' . http_build_query([
                    'rec_length' => $length,
                    'filter_ids' => '',
                    'uid' => $uid
                ]);
            tCurl::$defaultConfig = ['timeout' => 1];
            $rs = tCurl::Request('get', $url);
            $code = isset($rs['json']['code']) ? $rs['json']['code'] : 1;
            $data = isset($rs['json']['data']) ? $rs['json']['data'] : [];
            if ($code == 0) {
                $response = $data;
            } else {
                $msg = isset($rs['json']['msg']) ? 'url : ' . $url . $rs['json']['msg'] : '未知错误 url : ' . $url . ':' . json_encode($rs, JSON_UNESCAPED_UNICODE);
                throw new \Exception($msg);
            }
        } catch (\Exception $e) {
            //todo log
            self::errorLog('购物车推荐有误', [
                'line' => $e->getLine(),
                'message' => $e->getMessage(),
                'rs' => $rs,
                'args' => func_get_args()
            ]);
        }
        return $response;
    }

    /**
     * @desc 商城首页推荐算法
     * @param int $uid 用户id
     * @param int $length 推荐数量
     * @param array $filterGoodsIds 过滤商品id
     * @return array
     */
    public static function indexRecommendation($uid, $length, $filterGoodsIds)
    {
        $response = [];
        try {
            $domain = self::getDomain('user_recommend');
            $filter_ids = is_array($filterGoodsIds) && !empty($filterGoodsIds) ? implode(',', $filterGoodsIds) : '';
            $url = $domain . '?' . http_build_query([
                    'rec_length' => $length,
                    'filter_ids' => $filter_ids,
                    'uid' => $uid
                ]);
            tCurl::$defaultConfig = ['timeout' => 1];
            $rs = tCurl::Request('get', $url);

            $code = isset($rs['json']['code']) ? $rs['json']['code'] : 1;
            $data = isset($rs['json']['data']) ? $rs['json']['data'] : [];
            if ($rs['code'] == 1 && $code == 0) {
                $response = $data;
            } else {
                $msg = isset($rs['json']['msg']) ? 'url : ' . $url . $rs['json']['msg'] : '未知错误 url : ' . $url . ':' . json_encode($rs, JSON_UNESCAPED_UNICODE);
                self::errorLog('首页推荐错误1:' . $msg, [
                    'rs' => $rs,
                    'args' => func_get_args()
                ]);
            }
        } catch (\Exception $e) {
            self::errorLog('首页推荐错误2:' . $e->getMessage(), [
                'line' => $e->getLine(),
                'args' => func_get_args()
            ]);
        } finally {
            self::errorLog('首页推荐数据3:', [
                'response' => $response,
                'args' => func_get_args()
            ]);
            return $response;
        }
    }

}